import time, random, os, queue
import threading as td # 异步运行库
import Reloading

class WarehouseNone (Exception):
    pass
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
class ListNone (Exception):
    pass
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
class HaveError (Exception):
    pass
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def TheTime (operating_hour, sold_time, returns2):
    a = 0
    global the_time, stop_sold
    while (operating_hour):
        if (a):
            if (early_stop == 0):
                break
        time.sleep (15)
        a = 1
        operating_hour -= 1
        sold_time += 1
        the_time += 1
    stop_sold = 0
    while (all_break):
        time.sleep(0.1)
    returns2.put (sold_time)
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def CustomerCome (sold_customer: list, customer_need, all_sold_count, player_level, operating_goods: list, times_serve, player_xp, returns1, money, acc_num):
    c_num = 0
    earned_money = 0
    purchase_money = 0
    the_sever = 0
    the_count = 0
    print ("\n\033[32m正在等待顾客......\n")
    while (True):
        time.sleep (random.randint (1, 3))
        if (player_level < 3):
            c_num += random.uniform (0.4 * player_level, 0.5 * player_level)
        elif (8 > player_level >=3):
            c_num += random.uniform (0.15 * player_level, 0.2 * player_level)
        elif (10 > player_level >= 8):
            c_num += random.uniform (0.13 * player_level, 0.18 * player_level)
        elif (20 >= player_level >= 10):
            c_num += random.uniform (0.09 * player_level, 0.12 * player_level)
        if (player_level < 5):
            t = random.randint (1, 30)
        elif (10 > player_level >= 5):
            t = random.randint (1, 20)
        elif (player_level >= 1):
            t= random.randint (1, 15)
        if (c_num >= 1):
            c_num -= 1
            c = sold_customer[random.randint (0, len (sold_customer) - 1)]
            del sold_customer[sold_customer.index (c)]
            say = random.randint (1, 4)
            says = {1: "来啦！", 2: "光顾小店！", 3: "到来咯！", 4: "大驾光临！"}
            print (f"\033[32m{c} {says[say]}\033[0m\n")
            time.sleep (1)
            say = random.randint (1, 4)
            says = {1: "正在挑选商品……", 2: "说自己随便看看……", 3: "在思考要买什么……", 4: "要精打细算的花钱……"}
            print (f"\033[32m{c} {says[say]}\033[0m\n")
            time.sleep (2)
            if (t in [1, 2, 3]):
                c_s = c + str (t)
                for i in operating_goods:
                    if (i[0] == customer_need[c_s]):
                        if (i[5] == "\033[38;5;208m昂贵\033[0m"):
                            b = random.randint (1, 10)
                        elif (i[5] == "\033[39;32m实惠\033[0m"):
                            b = random.randint (1, 2)
                        elif (i[5] in ["\033[39;31m亏本\033[0m", "\033[38;5;246m平价\033[0m"]):
                            b = 1
                            if (b == 1):
                                say = random.randint (1, 3)
                                says = {1: "买定", 2: "全款买下", 3: "决定购买"}
                                count = random.randint (1, i[1])
                                print (f"\033[32m{c} {says[say]} {i[0]} {count}{i[2]}！")
                                print (f"\033[36m钱包到账\033[33m{int (i[3] * count * 1.5)}₿\033[0m\n")
                                earned_money += int (i[3] * count * 1.5)
                                purchase_money += int (i[4] * count)
                                operating_goods[operating_goods.index (i)][1] -= count
                                all_sold_count += count
                                the_count += count
                                if (operating_goods[operating_goods.index (i)][1] == 0):
                                    del operating_goods[operating_goods.index (i)]
                                times_serve += 1
                                the_sever += 1
                            else :
                                say = random.randint (1, 3)
                                says = {1: "离开了，Ta觉得", 2: "很失望，Ta认为", 3: "走了，Ta边走边说"}
                                say2 = random.randint (1, 3)
                                says2 = {1: "太贵了。", 2: "高于市场价。", 3: "买这些太奢侈了。"}
                                print (f"\033[31m{c} {says[say]} {i[0]} {says2[say2]}\033[0m\n")
                    elif (i == operating_goods[len (operating_goods) - 1]):
                        say = random.randint (1, 3)
                        says = {1: "离开了，Ta想要", 2: "很失望，Ta需要", 3: "没有中意的商品，Ta希望有"}
                        print (f"\033[31m{c} {says[say]} {customer_need[c_s]}。\033[0m\n")
            else :
                d = random.randint (1,5)
                i = operating_goods[random.randint (1, len (operating_goods)) - 1]
                if (d == 1 or (i[0] in [customer_need[c + '1'], customer_need[c + '2'], customer_need[c + '3']])):
                    say = random.randint (1, 2)
                    says = {1: "离开了，", 2: "走了，"}
                    say2 = random.randint (1, 3)
                    says2 = {1: "Ta只是随便逛逛。", 2: "Ta只是来看看。", 3: "Ta没有什么需要的。"}
                    print (f"\033[31m{c} {says[say]}{says2[say2]}\033[0m\n")
                elif (d > 1):
                    if (i[5] == "\033[38;5;208m昂贵\033[0m"):
                        b = random.randint (1, 40)
                    elif (i[5] == "\033[39;32m实惠\033[0m"):
                        b = random.randint (1, 3)
                    elif (i[5] in ["\033[39;31m亏本\033[0m", "\033[38;5;246m平价\033[0m"]):
                        b = 1
                    if (b in [1, 2]):
                        count = random.randint (1, i[1])
                        say = random.randint (1, 3)
                        says = {1: "买定", 2: "全款买下", 3: "决定购买"}
                        count = random.randint (1, i[1])
                        print (f"\033[32m{c} {says[say]} {i[0]} {count}{i[2]}！")
                        print (f"\033[36m钱包到账\033[33m{int (i[3] * count)}₿\033[0m\n")
                        earned_money += int (i[3] * count)
                        purchase_money += int (i[4] * count)
                        operating_goods[operating_goods.index (i)][1] -= count
                        all_sold_count += count
                        the_count += count
                        if (operating_goods[operating_goods.index (i)][1] == 0):
                            del operating_goods[operating_goods.index (i)]
                        times_serve += 1
                        the_sever += 1
                    else :
                        say = random.randint (1, 3)
                        says = {1: "离开了，Ta觉得", 2: "很失望，Ta认为", 3: "走了，Ta边走边说"}
                        say2 = random.randint (1, 3)
                        says2 = {1: "太贵了。", 2: "高于市场价。", 3: "买这些太奢侈了。"}
                        print (f"\033[32m{c} {says[say]} {i[0]} {says2[say2]}\033[0m\n")
            print ("\n\033[35m正在等待下一位顾客......\n")
        if (len (sold_customer) == 0 or len (operating_goods) == 0):
            global early_stop
            early_stop = 0
            time.sleep (15)
            if (len (sold_customer) == 0):
                os.system ('cls')
                print ("\033[38;5;216m今天不会再来其他客人了！")
            elif (len (operating_goods) == 0):
                print ("\033[38;5;216m上架的商品已经全部出售！")
            break
        if (stop_sold == 0):
            if (len (sold_customer) != 0 and len (operating_goods) != 0):
                print ("\033[38;5;216m营业时间结束！")
            break
    level_up_need_xp = [10, 20, 40, 80, 160,
                        260, 350, 500, 650, 800,
                        950, 980, 1000, 1000, 1000,
                        1000, 1000, 1000, 1000, 0]
    print ("\033[38;5;173m _.______._")
    print ("/          \\")
    print ("[\033[31mClose Shop\033[38;5;173m]\033[0m\n")
    time.sleep (0.3)
    print (f"\033[32m本日营业结算：")
    time.sleep (0.3)
    print (f"\033[0m营业\033[38;5;129m{the_time}\033[0m小时")
    time.sleep (0.3)
    print (f"成功接待\033[38;5;32m{the_sever}\033[0m名顾客")
    time.sleep (0.3)
    print (f"成功出售\033[38;5;32m{the_count}\033[0m单位商品")
    time.sleep (0.3)
    print (f"赚得\033[33m{earned_money}₿\033[0m")
    time.sleep (0.3)
    print (f"净利润\033[33m{earned_money - purchase_money}₿\033[0m")
    time.sleep (0.3)
    money += earned_money
    if (player_level == 20):
        print ("\033[32m已经满级了！\033[0m")
    elif (player_level != 20):
        player_xp += int (the_count / 2)
        print (f"共获得{int (the_count / 2)}点经验")
        while (True):
            i = float (level_up_need_xp[player_level - 1]) / 40.0
            x = int (player_xp / i)
            level_up = 0
            if (player_xp >= level_up_need_xp[player_level - 1]):
                player_xp -= level_up_need_xp[player_level - 1]
                player_level += 1
                level_up += 1
            elif (player_level == 20):
                print ("LEVEL UP!!!")
                print (f"已提升\033[33m{level_up}\033[0m级！")
                print ("\033[32m已经满级了！\033[0m")
            else :
                if (level_up):
                    print ("LEVEL UP!!!")
                    print (f"已提升\033[33m{level_up}\033[0m级！")
                print (f"当前\033[33m{player_level}\033[0m级")
                for i in range (x):
                    print ("\033[1;32m━", end = "")
                for i in range (40 - x):
                    print ("\033[1;37m━", end = "")
                print ("\033[0m")
                print (f"距离下一级还需{level_up_need_xp[player_level - 1] - player_xp}点经验")
                break
    time.sleep (0.3)
    if ((1 not in acc_num) and (earned_money - purchase_money >= 5000)):
        acc_num.append (1)
        print ("\n\033[33m达成成就《\033[38;5;219m日进斗金\033[33m》！\033[0m")
    if ((3 not in acc_num) and (player_level >= 20)):
        acc_num.append (3)
        print ("\n\033[33m达成成就《\033[38;5;219m小镇の经营明星\033[33m》！\033[0m")
    input ("\n按Enter继续...")
    global all_break
    all_break = 0
    returns1.put ((player_level, player_xp, operating_goods, all_sold_count, times_serve, money, acc_num))
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def Sold (player_level, customer_name: list, customer_need, all_sold_count, times_serve, sold_time, all_day_count, player_warehouse_list: list, player_warehouse_list_name: list, warehouse_level, original_price, price_up_amplitude, player_xp, money, acc_num): 
    # player_warehouse_list 格式如下：["名称", 单位数量, "单位"]
    os.system ('cls')
    all_goods_unit = warehouse_level * 20
    all_goods_type = warehouse_level * 2
    goods_unit = 0
    goods_type = 0
    operating_hour = 1
    operating_goods = [] # 格式如下：["名称", 单位数量, "单位", 玩家设定的售价, 原本进价, "昂贵/实惠/亏本"]
    while (True):
        try :
            print ("\033[33m|---------------------------------------")
            print (f"\033[33m|\033[36m营业时间：\033[0m{operating_hour}小时")
            print (f"\033[33m|\033[36m已上架\033[0m{goods_type}\033[36m种商品")
            print (f"\033[33m|\033[36m已上架\033[0m{goods_unit}\033[36m单位商品")
            print ("\033[33m|---------------------------------------")
            print (f"\033[33m|\033[38;5;210m当前还可上架\033[0m{all_goods_type - goods_type}\033[38;5;210m种商品")
            print (f"\033[33m|\033[38;5;210m当前还可上架\033[0m{all_goods_unit - goods_unit}\033[38;5;210m单位商品")
            print ("\033[33m|---------------------------------------")
            if (len (operating_goods) == 0):
                print (f"\033[33m|\033[0m无商品在货架上，请先上架商品后再开业！")
            else :
                num = 1
                for i in operating_goods:
                    print (f"\033[33m|\033[32m{num}. \033[0m{i[0]} {i[1]}{i[2]} \033[32m售价:\033[33m{i[3]}₿ \033[35m进价:\033[33m{i[4]}₿ \033[0m定价接受度：{i[5]}")
                    num += 1
            print ("\033[33m|---------------------------------------")
            print (f"\033[33m|\033[34mTi. 修改营业时长")
            print (f"\033[33m|\033[36mAd. 编辑商品")
            print (f"\033[33m|\033[32mGo. 开始营业")
            print (f"\033[33m|\033[31mBc. 退出营业")
            print ("\033[33m|---------------------------------------\033[0m")
            chos = input ("输入需要的功能：")
            if (chos == 'Ti'):
                operating_hour = SetTime (operating_hour, player_level)
            elif (chos == 'Ad'):
                player_warehouse_list, player_warehouse_list_name, operating_goods, original_price, goods_unit, goods_type = AddDeleteList (player_warehouse_list, player_warehouse_list_name, operating_goods, original_price, goods_unit, goods_type, warehouse_level, price_up_amplitude)
            elif (chos == 'Go'):
                if (len (operating_goods) > 0):
                    os.system ('cls')
                    break
                else :
                    os.system ('cls')
                    print ("请先上架商品再开业！")
            elif (chos == 'Bc'):
                os.system ('cls')
                for i in operating_goods :
                    if (i[0] in player_warehouse_list_name):
                        x = player_warehouse_list_name.index (i[0])
                        player_warehouse_list[x][1] += i[1]
                    elif (i[0] not in player_warehouse_list_name):
                        player_warehouse_list.append ([i[0], i[1], i[2]])
                        player_warehouse_list_name.append (i[0])
                        original_price.append (i[4])
                back = False
                return player_level, times_serve, sold_time, all_day_count, all_sold_count, player_warehouse_list, player_warehouse_list_name, warehouse_level, original_price, player_xp, back, money, acc_num
        except ValueError:
            os.system ('cls')
            print ("\033[1;31m无效选项！\033[0m\n")
    
    sold_customer = []
    if (7 > player_level >= 1):
        sold_customer = customer_name.copy ()
    elif (10 > player_level > 7):
        random.shuffle (customer_name)
        for i in range (player_level):
            sold_customer.append (customer_name[i])
    elif (player_level >= 10):
        random.shuffle (customer_name)
        for i in range (player_level * 2):
            sold_customer.append (customer_name[i])
    
    global stop_sold, the_time, all_break, early_stop
    stop_sold = 1
    the_time = 0
    all_break = 1
    early_stop = 1
    
    print ("\033[38;5;173m _.______._")
    print ("/          \\")
    print ("[\033[32mOpen  Shop\033[38;5;173m]\033[0m")
    print ("\033[32m开始营业！\033[0m\n")
    
    returns1 = queue.Queue ()
    returns2 = queue.Queue ()
    
    sold1 = td.Thread (target = TheTime, args = (operating_hour, sold_time, returns2))
    sold2 = td.Thread (target = CustomerCome, args = (sold_customer, customer_need, all_sold_count, player_level, operating_goods, times_serve, player_xp, returns1, money, acc_num))
    
    sold1.start ()
    sold2.start ()
    
    sold1.join ()
    sold2.join ()
    
    player_level, player_xp, operating_goods, all_sold_count, times_serve, money, acc_num = returns1.get ()
    sold_time = returns2.get ()
    
    for i in operating_goods :
        if (i[0] in player_warehouse_list_name):
            x = player_warehouse_list_name.index (i[0])
            player_warehouse_list[x][1] += i[1]
        elif (i[0] not in player_warehouse_list_name):
            player_warehouse_list.append ([i[0], i[1], i[2]])
            player_warehouse_list_name.append (i[0])
            original_price.append (i[4])
    if (None in player_warehouse_list_name):
        del player_warehouse_list_name[player_warehouse_list_name.index (None)]
    back = True
    os.system ('cls')
    Reloading.SortingShopLoading ()
    return player_level, times_serve, sold_time, all_day_count, all_sold_count, player_warehouse_list, player_warehouse_list_name, warehouse_level, original_price, player_xp, back, money, acc_num
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
def SetTime (operating_hour, player_level):
    os.system ('cls')
    while (True):
        try :
            print ("\033[33m|---------------------------------------")
            print (f"\033[33m|\033[36m当前营业时间：\033[0m{operating_hour}小时")
            if (player_level == 1):
                print (f"\033[33m|\033[31m当前玩家等级只能营业1小时！")
            elif (12 >= player_level > 1):
                print (f"\033[33m|\033[31m当前玩家等级可修改营业时间范围为1-{player_level}小时")
            elif (player_level > 12):
                print (f"\033[33m|\033[31m当前玩家等级可修改营业时间范围为1-12小时")
            print ("\033[33m|---------------------------------------")
            if (player_level > 1):
                print ("\033[33m|\033[32mx. 输入对应小时数字即可更改营业时长")
                print ("\033[33m|\033[31m0. 返回")
                print ("\033[33m|---------------------------------------\033[0m")
                chos = int (input ("输入营业时间："))
                if (((12 > player_level > 1) and (player_level >= chos >=1)) or ((player_level >= 12) and (12 >= chos >=1))):
                    operating_hour = chos
                    os.system ('cls')
                    print (f"\033[32m已成功将营业时间改为{chos}小时")
                elif (chos == 0):
                    os.system ('cls')
                    return operating_hour
                else :
                    raise ValueError
            else :
                input ("\n按Enter返回...")
                os.system ('cls')
                return operating_hour
        except ValueError:
            os.system ('cls')
            print ("\033[1;31m超出可营业时间范畴！\033[0m\n")
# ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
# player_warehouse_list 格式如下：["名称", 单位数量, "单位"]
# operating_goods 格式如下：["名称", 单位数量, "单位", 玩家设定的售价, 原本进价, "昂贵/实惠/亏本"]
def AddDeleteList (player_warehouse_list: list, player_warehouse_list_name: list, operating_goods: list, original_price: list, goods_unit: int, goods_type: int, warehouse_level: int, price_up_amplitude: list):
    f = price_up_amplitude
    mode = True
    name = []
    if (len (operating_goods) > 0):
        for i in operating_goods:
            name.append (i[0])
    os.system ('cls')
    while (True):
        if (None in player_warehouse_list_name):
            del player_warehouse_list_name[player_warehouse_list_name.index (None)]
        if (len (operating_goods) == 0):
            mode = True
        elif (len (player_warehouse_list_name) == 0):
            mode = False
        try :
            print ("\033[33m|---------------------------------------")
            print ("\033[33m|\033[0m已上架商品列表：")
            num = 1
            if (len (operating_goods) > 0 and not mode):
                for i in operating_goods:
                    print (f"\033[33m|\033[39;31m{num}. \033[0m{i[0]} {i[1]}{i[2]} 售价:{i[3]}₿ 进价:{i[4]}₿ 定价接受度：{i[5]}")
                    num += 1
            elif (len (operating_goods) > 0 and mode):
                for i in operating_goods:
                    print (f"\033[33m|\033[0m{num}. {i[0]} {i[1]}{i[2]} 售价:{i[3]}₿ 进价:{i[4]}₿ 定价接受度：{i[5]}")
                    num += 1
            else :
                print ("\033[33m|\033[0m无商品在货架上。")
            print ("\033[33m|---------------------------------------")
            print ("\033[33m|\033[0m仓库内库存商品：")
            num = 1
            if (len (player_warehouse_list_name) > 0 and mode):
                for i in player_warehouse_list:
                    print (f"\033[33m|\033[39;32m{num}. \033[0m{i[0]} {i[1]}{i[2]} 进价:{original_price[num - 1]}₿")
                    num += 1
            elif (len (player_warehouse_list) > 0 and not mode):
                for i in player_warehouse_list:
                    print (f"\033[33m|\033[0m{num}. {i[0]} {i[1]}{i[2]} 进价:{original_price[num - 1]}₿")
                    num += 1
            else :
                print ("\033[33m|\033[0m仓库已被清空！")
            print ("\033[33m|---------------------------------------")
            if (warehouse_level * 20 - goods_unit != 0):
                print (f"\033[33m|\033[38;5;210m还可上架\033[0m{warehouse_level * 2 - goods_type}\033[38;5;210m种商品")
                print (f"\033[33m|\033[38;5;210m还可上架\033[0m{warehouse_level * 20 - goods_unit}\033[38;5;210m单位商品")
            else :
                print (f"\033[33m|\033[38;5;210m共上架\033[0m{goods_type}\033[38;5;210m种商品")
                print (f"\033[33m|\033[38;5;210m共上架\033[0m{warehouse_level * 20}\033[38;5;210m单位商品")
                print (f"\033[33m|\033[31m货架已满！")
            print ("\033[33m|---------------------------------------")
            mode_lise = {True: '\033[39;32m上架商品\033[0m', False: '\033[39;31m下架商品\033[0m'}
            print (f"\033[33m|\033[0m当前模式为\"{mode_lise[mode]}\"")
            print (f"\033[33m|\033[32mx. 输入有色序号以对应模式编辑商品")
            if (mode):
                print ("\033[33m|\033[31mD. \033[0m切换为\"\033[31m下架商品\033[0m\"模式")
            elif (not mode):
                print ("\033[33m|\033[32mA. \033[0m切换为\"\033[32m上架商品\033[0m\"模式")
            print ("\033[33m|\033[31m0. 返回")
            print ("\033[33m|---------------------------------------\033[0m")
            chos = input ("输入操作序号：")
            os.system ('cls')
            if (len (player_warehouse_list_name) == 0):
                player_warehouse_list_name = [None]
            if (chos in ['D', 'A']):
                if (chos == 'D' and mode and len (operating_goods) > 0):
                    mode = False
                elif (chos == 'D' and mode and len (operating_goods) == 0):
                    raise ListNone
                elif (chos == 'A' and not mode and player_warehouse_list_name[0] != None):
                    mode = True
                elif (chos == 'A' and not mode and player_warehouse_list_name[0] == None):
                    raise WarehouseNone
            elif (int (chos) == 0):
                os.system ('cls')
                return player_warehouse_list, player_warehouse_list_name, operating_goods, original_price, goods_unit, goods_type
            elif ((int (chos) > len (operating_goods) and not mode) or (int (chos) > len (player_warehouse_list) and mode)):
                raise HaveError
            elif ((int (chos) < 0)):
                raise ValueError
            elif (mode):
                if (goods_unit == warehouse_level * 20 and len (operating_goods) == warehouse_level * 2):
                    os.system ('cls')
                    print ("\033[1;31m货架可容纳单位数量已经满啦！\033[0m\n")
                    print ("\033[1;31m货架可容纳种类已经满啦！\033[0m\n")
                    input ("按Enter继续...")
                elif (goods_unit == warehouse_level * 20):
                    os.system ('cls')
                    print ("\033[1;31m货架可容纳单位数量已经满啦！\033[0m\n")
                    input ("按Enter继续...")
                elif (len (operating_goods) == warehouse_level * 2):
                        os.system ('cls')
                        print ("\033[1;31m货架可容纳种类已经满啦！\033[0m\n")
                        input ("按Enter继续...")
                elif (player_warehouse_list_name[int (chos) - 1] in name):
                    if (player_warehouse_list_name[0] == None):
                        del player_warehouse_list_name[0]
                    raise IndexError
                elif (player_warehouse_list[int (chos) - 1][0] not in name): # 上架
                    if (len (operating_goods) != warehouse_level * 2 and goods_unit != warehouse_level * 20):
                        i = player_warehouse_list[int (chos) - 1]
                        p = original_price[int (chos) - 1]
                        print (f"\033[32m正在编辑上架\033[0m{i[0]} {i[1]}{i[2]}")
                        print (f"\033[31m至少上架1{i[2]}，且为整数，且在已有的{i[2]}数内！")
                        count = int (input (f"你要上架多少{i[2]}:\033[0m"))
                        c = 0
                        if (len (operating_goods) > 0):
                            for d in operating_goods:
                                c += d[1]
                        if (c + count > warehouse_level * 20):
                            os.system ('cls')
                            print ("\033[1;31m超出货架可容纳单位数量啦！\033[0m\n")
                            input ("按Enter继续...")
                        elif (count > i[1]):
                            os.system ('cls')
                            print ("\033[1;31m超出该商品持有单位数量！\033[0m\n")
                            input ("按Enter继续...")
                        elif (i[1] >= count >= 1):
                            os.system ('cls')
                            print ("\033[31m建议定价在盈利的同时不要过高！")
                            print (f"{i[0]} \033[32m进价:\033[33m{p}₿")
                            price = int (input ("\033[31m请定价:\033[0m"))
                            if (p < 10):
                                if (price - p > 1):
                                    extent = "\033[38;5;208m昂贵\033[0m"
                                elif (price - p == 1):
                                    extent = "\033[39;32m实惠\033[0m"
                                elif (price - p == 0):
                                    extent = "\033[38;5;246m平价\033[0m"
                                elif (price - p < 0):
                                    extent = "\033[39;31m亏本\033[0m"
                            elif (50 > p > 10):
                                if (float (price) / float (p) > 1.1):
                                    extent = "\033[38;5;208m昂贵\033[0m"
                                elif (0 < float (price) / float (p) < 1.1):
                                    extent = "\033[39;32m实惠\033[0m"
                                elif (price - p == 0):
                                    extent = "\033[38;5;246m平价\033[0m"
                                elif (price - p < 0):
                                    extent = "\033[39;31m亏本\033[0m"
                            elif (500 > p > 50):
                                if (float (price) / float (p) > f[1]):
                                    extent = "\033[38;5;208m昂贵\033[0m"
                                elif (0 < float (price) / float (p) < f[1]):
                                    extent = "\033[39;32m实惠\033[0m"
                                elif (price - p == 0):
                                    extent = "\033[38;5;246m平价\033[0m"
                                elif (price - p < 0):
                                    extent = "\033[39;31m亏本\033[0m"
                            elif (1500 > p > 501):
                                if (float (price) / float (p) > f[2]):
                                    extent = "\033[38;5;208m昂贵\033[0m"
                                elif (0 < float (price) / float (p) < f[2]):
                                    extent = "\033[39;32m实惠\033[0m"
                                elif (price - p == 0):
                                    extent = "\033[38;5;246m平价\033[0m"
                                elif (price - p < 0):
                                    extent = "\033[39;31m亏本\033[0m"
                            elif (p > 1501):
                                if (float (price) / float (p) > f[3]):
                                    extent = "\033[38;5;208m昂贵\033[0m"
                                elif (0 < float (price) / float (p) < f[3]):
                                    extent = "\033[39;32m实惠\033[0m"
                                elif (price - p == 0):
                                    extent = "\033[38;5;246m平价\033[0m"
                                elif (price - p < 0):
                                    extent = "\033[39;31m亏本\033[0m"
                            os.system ('cls')
                            print (f"{i[0]} {count}{i[2]} 售价:{price}₿ \033[32m已上架！\033[0m")
                            if (count == i[1]):
                                operating_goods.append ([i[0], i[1], i[2], price, p, extent])
                                del player_warehouse_list[int (chos) - 1], player_warehouse_list_name[int (chos) - 1], original_price[int (chos) - 1]
                            elif (count < i[1]):
                                operating_goods.append ([i[0], count, i[2], price, p, extent])
                                player_warehouse_list[int (chos) - 1][1] -= count
                            name.append (i[0])
                            goods_type += 1
                            goods_unit += count
            elif (not mode):
                if (len (operating_goods) == 0):
                    raise ValueError
                else : # 下架
                    i = operating_goods[int (chos) - 1]
                    os.system ('cls')
                    print (f"\033[31m已成功下架\033[0m{i[0]} {i[1]}{i[2]} 售价:{i[3]}₿")
                    if (i[0] in player_warehouse_list_name):
                        x = player_warehouse_list_name.index (i[0])
                        player_warehouse_list[x][1] += i[1]
                    elif (i[0] not in player_warehouse_list_name):
                        player_warehouse_list.append ([i[0], i[1], i[2]])
                        player_warehouse_list_name.append (i[0])
                        original_price.append (i[4])
                    goods_type -= 1
                    goods_unit -= i[1]
                    del operating_goods[int (chos) - 1], name[int (chos) - 1]
            else :
                raise IndexError
        except ValueError:
            os.system ('cls')
            print ("\033[1;31m无效编辑！\033[0m\n")
            input ("按Enter继续...")
        except IndexError:
            os.system ('cls')
            print ("\033[1;31m该商品已在货架上，若需改变单位数量或价格请先下架！\033[0m\n")
            input ("按Enter继续...")
        except WarehouseNone:
            os.system ('cls')
            print ("\033[1;31m仓库已经清仓！\033[0m\n")
            input ("按Enter继续...")
        except ListNone:
            os.system ('cls')
            print ("\033[1;31m货架上没有商品！\033[0m\n")
            input ("按Enter继续...")
        except HaveError:
            os.system ('cls')
            print ("\033[1;31m无该商品！\033[0m\n")
            input ("按Enter继续...")